//
//  LoopView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/24.
//

import UIKit

class LoopView: UIView {

    lazy var scroView: UIScrollView = {
        let view = UIScrollView()
        view.isScrollEnabled = false
        return view
    }()
    
    var timer: Timer?
    
    var timerCount: Float = 0.0
    
    var second = 2
        
    var userViewArr = [LoopUserView]()

    var modelArr = [LotteryModel]() {
        didSet {
            startLoopView()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .init(named: "purple_AB95FE")
        addSubview(scroView)
        scroView.snp.makeConstraints { make in
            make.top.left.bottom.right.equalTo(0)
        }
        wn_cornerWithBorder(20)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startLoopView() {
        removeTimer()
        scroView.contentOffset = .zero
        userViewArr.removeAll()
        for i in 0..<modelArr.count {
            let view = LoopUserView(frame: .init(x: 0, y: 40*CGFloat(i), width: ScreenW-96, height: 40))
            view.model = modelArr[i]
            userViewArr.append(view)
            scroView.addSubview(view)
        }
        scroView.contentSize = .init(width: ScreenW-96, height: CGFloat(modelArr.count)*40)
        addTimer()
    }
    
    func addTimer() {
        if timer != nil {
            removeTimer()
            return
        }
        timer = Timer.init(timeInterval: 0.01, target: self, selector: #selector(timerFired), userInfo: nil, repeats: true)
        RunLoop.main.add(timer!, forMode: .common)
    }
    
    @objc func timerFired() {
        if (scroView.contentOffset.y > CGFloat(modelArr.count)*40) {
            scroView.contentOffset = .zero
        }else {
            scroView.contentOffset = .init(x: 0, y: scroView.contentOffset.y+0.17)
        }
    }
    
    func removeTimer() {
        if timer != nil {
            timer?.invalidate()
            timer = nil
        }
    }
}

class LoopUserView: UIView {
    
    lazy var headerImage: UIImageView = {
        let view = UIImageView()
        view.contentMode = .scaleAspectFill
        view.wn_cornerWithBorder(14)
        return view
    }()
    
    lazy var looplabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .regular, size: 14)
        label.textColor = .init(named: "black_333333")
        label.textAlignment = .center
        return label
    }()
    
    lazy var goldImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "reward_coin"))
        return image
    }()

    var model: LotteryModel? {
        didSet {
            if let m = model {
                headerImage.wn_setupImg(m.avatar, placeHolder: "placeholder_header")
                let str = "\(m.name ?? "username") got \(m.gold)"
                looplabel.attributedText = String().wn_textChangeColor(str, "\(m.gold)", "",  .init(named: "red_FE95B4")!, .wn_font(name: .bold, size: 16))
                layoutIfNeeded()
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(looplabel)
        addSubview(headerImage)
        addSubview(goldImage)
        looplabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        headerImage.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(looplabel.snp.left).offset(-8)
            make.height.width.equalTo(28)
        }
        goldImage.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(looplabel.snp.right).offset(4)
            make.width.height.equalTo(16)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
